package data;

import java.util.Date;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class PackageByDate extends AbstractData {
	private long id = -1;
	private Package packages;
	private String packageString;
	private Date dateFrom;
	private Date dateTo;
	
	public PackageByDate(){
		
	}
	
	public PackageByDate(long id){
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Package getPackages() {
		if (packages == null && !(packageString == null || packageString.isEmpty())) {
			Package r = new Package(Long.parseLong(packageString));
			r.LoadData();
			packages = r;
		}
		return packages;
	}

	public void setPackages(Package packages) {
		this.packageString = String.format("%d", packages.getId());
		this.packages = packages;
	}

	public String getPackageString() {
		return packageString;
	}

	public void setPackageString(String packString) {
		this.packageString = packString;
	}

	public Date getDateFrom() {
		return dateFrom;
	}

	public void setDateFrom(Date dF) {
		this.dateFrom = dF;
	}

	public Date getDateTo() {
		return dateTo;
	}

	public void setDateTo(Date dateTo) {
		this.dateTo = dateTo;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.PackageByDate);
		else
			item = new Entity(this.key);

		item.setProperty("dateFrom", this.dateFrom);
		item.setProperty("dateTo", this.dateTo);
		item.setProperty("package", this.packageString);
		return item;
	}

	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.dateFrom = (Date) entity.getProperty("dateFrom");
		this.dateTo = (Date) entity.getProperty("dateTo");
		this.packageString = (String) entity.getProperty("package");
		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.PackageByDate, this.id);
			setEntity(entity);
		}
	}
}
